﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Serialization;
using System.Windows.Markup;


namespace MutFormStudio.test {
	public partial class Form1 : Form {
		public Form1() {
			InitializeComponent();
			mainStudio.Base_studio.eControlsClosing += MainStudio_eControlsClosing;
			mainStudio.addControl(new testControl($"原有组件 {next_form_num++}", Color.Plum));
			mainStudio.addControl(new testControl($"原有组件 {next_form_num++}", Color.Pink));
			mainStudio.addControl(new testControl($"原有组件 {next_form_num++}", Color.Pink));
			mainStudio.Split(SplitDirection.Right);
			mainStudio.Second_studio.addControl(new testControl($"原有组件 {next_form_num++}", Color.GreenYellow));
			mainStudio.Second_studio.addControl(new testControl($"原有组件 {next_form_num++}", Color.SpringGreen)) ;
			mainStudio.Main_studio.addControl(new testControl($"原有组件 {next_form_num++}", Color.Khaki));
		}

		private void MainStudio_eControlsClosing(object sender, ControlsCloseEventArgs e) {
			StringBuilder sb = new StringBuilder();
			sb.Append("即将关闭多个窗体：\n");
			foreach (var flag in e.Close_flags) {
				sb.Append($"\t{flag.Control.get_Head_text() }\n");
			}
			DialogResult result = MessageBox.Show(this, sb.ToString(), "关闭多个窗体", MessageBoxButtons.YesNo);
			if(result == DialogResult.Yes) {
				foreach (var flag in e.Close_flags) {
					Console.WriteLine($"关闭了{flag.Control.get_Head_text() }");
				}
			} else {
				foreach (var flag in e.Close_flags) {
					flag.Cancle = true;
				}
			}
		}

		int next_form_num = 1;
		private void 添加测试的组件ToolStripMenuItem_Click(object sender, EventArgs e) {
			this.mainStudio.addControl(new testControl($"新组件 {next_form_num++}",Color.Aquamarine) );
		}
		string last_xml;
		private void toXaml(object sender, EventArgs e) {
			var sb = new StringBuilder();
			var xaml_obj = mainStudio.ToXaml();
			xaml_obj.Sub_studios[0].Form = new ToXaml.Form(this);
			using (var xml = XmlWriter.Create(sb)) {
				System.Windows.Markup.XamlWriter.Save(xaml_obj, xml);
			}
			last_xml = sb.ToString() ;
			Console.WriteLine(last_xml);
		}

		private void 从xml创建窗体ToolStripMenuItem_Click(object sender, EventArgs e) {
			var xaml_obj = System.Windows.Markup.XamlReader.Parse(last_xml) as ToXaml.Studio;
			var newForm = new Form2(xaml_obj);
			newForm.Show();
			xaml_obj.Sub_studios[0].Form.setToForm(newForm);
		}

	}
}
